package net.toby200.canalbuddy.notices;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import net.toby200.canalbuddy.R;
import net.toby200.canalbuddy.notices.model.Notice;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by toby on 18/07/2014.
 */
public class NoticeFilter {

    private final Context context;

    
    public NoticeFilter(Context context) {
        this.context = context;
    }


    public Collection<Notice> getFilteredNotices(Collection<Notice> notices) {
        return filterCanalPreferences(notices);
    }

    /**
     *
     * @param notices
     * @return Any notice where the name of the waterway contains the full name of the filter value
     */
    private Collection<Notice> filterCanalPreferences(Collection<Notice> notices) {

        List<Notice> filteredNotices = new ArrayList<Notice>();

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        Set<String> canalsFilter = prefs.getStringSet(context.getString(R.string.canal_pref_key), new HashSet<String>());

        for (Notice notice : notices) {
            boolean canalOk = false;
            for(String canal : canalsFilter) {
                if (notice.getWaterway().contains(canal)) {
                    canalOk = true;
                    break;
                }
            }

            if (canalOk) {
                filteredNotices.add(notice);
            }
        }

        return filteredNotices;
    }

}
